package com.mbc.web.common.client.ui.control;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.datepicker.client.DateBox;

public class MDateBox extends DateBox {
	
	private HandlerRegistration hr;
	
	public MDateBox() {
		ValidationDateBox(true);
	}
	
	public void setRequired(boolean required)
	{
		if(required)
			this.getElement().setAttribute("required", "");
		else
			this.getElement().removeAttribute("required");
	}
	
	public void setPlaceholder(String placeholder){
		this.getElement().setAttribute("placeholder", placeholder);
	}
	
	public void setTabindex(String tabindex){
		this.getElement().setAttribute("tabindex", tabindex);
	}
	public void ValidationDateBox(boolean isUse) {
		if(isUse) {
			hr = getTextBox().addKeyPressHandler(new KeyPressHandler() {
				
				@Override
				public void onKeyPress(KeyPressEvent event) {
					event.preventDefault();
					
					try {
						int code = 0;
						if(event.getNativeEvent().getKeyCode() != 0)
							code = event.getNativeEvent().getKeyCode();
						else code = event.getCharCode();
						
						int getCursorPosition = getTextBox().getCursorPos();
						String value = getTextBox().getValue();
						String input = "", front, back;
						
						if(code == 48) input = "0";
						else if(code == 49) input = "1";
						else if(code == 50) input = "2";
						else if(code == 51) input = "3";
						else if(code == 52) input = "4";
						else if(code == 53) input = "5";
						else if(code == 54) input = "6";
						else if(code == 55) input = "7";
						else if(code == 56) input = "8";
						else if(code == 57) input = "9";
						else if(code == 8) {
							if(getTextBox().getSelectionLength() == 10) {
								getTextBox().setText("");
								getTextBox().setText("__/__/____");
								getTextBox().setCursorPos(0);
								return;
							} else if(getTextBox().getSelectionLength() > 0) {
								String total_val = "";
								char[] getcharArray = getTextBox().getSelectedText().toCharArray();
								for(int i = 0; i < getcharArray.length; i++) {
									String str = "" + getcharArray[i];
									if(!str.equals("/"))
										total_val += "_";
									else
										total_val += "/";
								}
								
								front = value.substring(0, getCursorPosition); 
								back = value.substring(getTextBox().getCursorPos() + getTextBox().getSelectionLength(), value.length());
								getTextBox().setValue(front + total_val + back);
								getTextBox().setCursorPos(getCursorPosition);
								
								return;
							}
							else input = "_";
							if(getCursorPosition != 0) getCursorPosition--;
						}
						else if(code == 46) {
							if(getTextBox().getSelectionLength() == 10) {
								getTextBox().setText("");
								getTextBox().setText("__/__/____");
								getTextBox().setCursorPos(0);
								return;
							}
							else input = "_";
						}
						else if(code == KeyCodes.KEY_RIGHT) {
							getTextBox().setCursorPos(getCursorPosition + 1);
							return;
						}
						else if(code == KeyCodes.KEY_LEFT) {
							getTextBox().setCursorPos(getCursorPosition - 1);
							return;
						}
						else return;
						
						if(getCursorPosition <= 9) {		
							if(getCursorPosition == 2 || getCursorPosition == 5) {
								if(code == 8) input = "/";
								else getCursorPosition++;
							}
							
							front = value.substring(0, getCursorPosition); 
							back = value.substring(getCursorPosition + 1, value.length());
							if(!input.equals("")) getTextBox().setValue(front + input + back);
							if(code != 8) getTextBox().setCursorPos(getCursorPosition + 1);
							else getTextBox().setCursorPos(getCursorPosition);
						}
					}catch(Exception ex) { }
				}
			});
		} else
			hr.removeHandler();
	}
}
